package com.yanqu.road.server.cmd.activity.yearMaster;

import com.yanqu.road.entity.activity.yearMaster.UserYearMasterDataResult;
import com.yanqu.road.logic.pb.YearMasterPb;
import com.yanqu.road.pb.yearMaster.YearMasterProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.yearMaster.YearMasterModule;
import com.yanqu.road.server.manger.activity.yearMaster.YearMasterMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;


@Cmd(code = GameProtocol.S_YEAR_MASTER_USE_TOOL, desc = "瑞兽降福使用道具")
public class YearMasterUseGoodsCmd extends AbstractCommand {

    private int goodsId;
    private int goodsNum;
    private UserYearMasterDataResult userYearMasterDataResult;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        YearMasterProto.YearMasterUseGoodsReqMsg reqMsg = YearMasterProto.YearMasterUseGoodsReqMsg.parseFrom(packet.getBytes());
        goodsId = reqMsg.getGoodsId();
        goodsNum = reqMsg.getGoodsNum();

        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        userYearMasterDataResult = player.getModule(YearMasterModule.class).useGoods(goodsId, goodsNum);
    }

    @Override
    public void sendData(GamePlayer player) {
        YearMasterProto.YearMasterUseGoodsRspMsg.Builder rspMsg = YearMasterProto.YearMasterUseGoodsRspMsg.newBuilder();
        rspMsg.setRet(userYearMasterDataResult.getRet());
        if (userYearMasterDataResult.getUserYearMasterData() != null) {
            rspMsg.setBaseInfo(YearMasterPb.parseYearMasterBaseRespMsg(YearMasterMgr.getYearMasterBoss(), userYearMasterDataResult.getUserYearMasterData(),YearMasterMgr.getYearMasterActivityConfig().getHighOpenScore()));
        }
        player.sendPacket(ClientProtocol.U_YEAR_MASTER_USE_TOOL, rspMsg);

    }
}
